import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * @Author Stringzhua
 * @Date 2024/10/30 12:04
 * description:
 */
public class ThreadPool02 {
    public static void main(String[] args) {
        //计算1~100w之间所有数字的累加和
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);


        ArrayList<Future<Integer>> futureList = new ArrayList<>();
        //拆分成10个线程任务
        //提交线程任务
        for (int i = 1; i < 900000; i += 100000) {
            Future<Integer> future = fixedThreadPool.submit(new AddTask(i, i + 100000));
            futureList.add(future);
        }

        Integer total = 0;
        try {
            for (Future<Integer> future : futureList) {
                Integer num = future.get();
                total += num;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(total);

        fixedThreadPool.shutdown();
    }
}

class AddTask implements Callable<Integer> {
    private int begin, end;

    public AddTask(int begin, int end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    public Integer call() throws Exception {
        int result = 0;
        for (int i = begin; i <= end; i++) {
            result += i;
        }
        return result;
    }
}
